
package com.sgame.android.network;

import java.io.*;
import java.util.Random;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.message.BasicHeader;

public final class HttpOutputStream
    implements HttpEntity
{

    public HttpOutputStream()
    {
        e = null;
        _outStream = new ByteArrayOutputStream();
        b = false;
        c = false;
        StringBuffer stringbuffer = new StringBuffer();
        Random random = new Random();
        for(int i = 0; i < 30; i++)
            stringbuffer.append(d[random.nextInt(d.length)]);

        e = stringbuffer.toString();
    }

    public final void a(String name, String fileName, InputStream inputstream)
    {
        String s2;
        StringBuilder sb;
        if(c)
            return;
        byte [] bytes = new byte[4096];
        int len;
        try {
            _outStream.write((new StringBuilder("--")).append(e).append("\r\n").toString().getBytes());
            c = true;
            s2 = "Content-Type: application/octet-stream\r\n";
            sb = new StringBuilder("");
            sb.append("Content-Disposition: form-data; name=\"");
            sb.append(name).append("\"; filename=\"").append(fileName).append("\"\r\n");
            _outStream.write(sb.toString().getBytes());
            _outStream.write(s2.getBytes());
            _outStream.write("\r\n".getBytes());
			while((len = inputstream.read(bytes)) > 0)
			    _outStream.write(bytes, 0, len);
	        _outStream.flush();
	        inputstream.close();
		}
        catch (IOException e1) {
			e1.printStackTrace();
		}

        return;
    }

    public final void a(String s, File file)
    {
        try {
			a(s, file.getName(), ((InputStream) (new FileInputStream(file))));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
    }

    public final long getContentLength()
    {
        if(b)
            return 0;
        try {
			_outStream.write((new StringBuilder("\r\n--")).append(e).append("--\r\n").toString().getBytes());
	        b = true;
	        return (long)_outStream.toByteArray().length;
		} catch (IOException e) {
			e.printStackTrace();
		}
        return 0;
    }

    public final Header getContentType()
    {
        return new BasicHeader("Content-Type", (new StringBuilder("multipart/form-data; boundary=")).append(e).toString());
    }

    public final boolean isChunked()
    {
        return false;
    }

    public final boolean isRepeatable()
    {
        return false;
    }

    public final boolean isStreaming()
    {
        return false;
    }

    public final void writeTo(OutputStream outputstream)
        throws IOException
    {
        outputstream.write(_outStream.toByteArray());
    }

    public final Header getContentEncoding()
    {
        return null;
    }

    public final void consumeContent()
        throws IOException, UnsupportedOperationException
    {
        if(isStreaming())
            throw new UnsupportedOperationException("Streaming entity does not implement #consumeContent()");
        else
            return;
    }

    public final InputStream getContent()
        throws IOException, UnsupportedOperationException
    {
        return new ByteArrayInputStream(_outStream.toByteArray());
    }

    private static final char d[] = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private String e;
    ByteArrayOutputStream _outStream;
    boolean b;
    boolean c;

}
